home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ddj0897.zip / DYN401.ZIP / dpp / wildargv.c < prev   
C/C++ Source or Header  |  1996-02-04  |  4KB  |  159 lines

  1. /*
  2.  *%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3.  *%      Copyright (C) 1989, by WATCOM Systems Inc. All rights     %
  4.  *%      reserved. No part of this software may be reproduced        %
  5.  *%      in any form or by any means - graphic, electronic or        %
  6.  *%      mechanical, including photocopying, recording, taping     %
  7.  *%      or information storage and retrieval systems - except     %
  8.  *%      with the written permission of WATCOM Systems Inc.        %
  9.  *%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  10.   WILDARGV - split DOS command line into individual arguments expanding
  11.          those that contain ? or *.
  12.   This module is a substitute for the "initargv" module contained in the
  13.   library.
  14.  
  15.   Modified:    By:        Reason:
  16.   ---------    ---        -------
  17.   23-aug-89    John Dahms    was ignoring files with Archive or
  18.                 read only attributes turned on. (Bug fix)
  19.   15-sep-91    F.W.Crigger    Use _LpCmdLine, _LpPgmName, _argc, _argv,
  20.                   ___Argc, ___Argv
  21.   13-jul-92    John Dahms    add (void near *) cast in _allocate
  22.   02-nov-93    A.F.Scian    fixed so that it compiles as C++
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <io.h>
  28. #include <direct.h>
  29. #include <malloc.h>
  30.  
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34.  
  35. extern    void    _Not_Enough_Memory();
  36. extern    char    *_LpCmdLine;
  37. extern    char    *_LpPgmName;
  38. extern    int    _argc;            /* argument count  */
  39. extern    char  **_argv;            /* argument vector */
  40. extern    int    ___Argc;        /* argument count */
  41. extern    char  **___Argv;        /* argument vector */
  42.  
  43. #ifdef __cplusplus
  44. };
  45. #endif
  46.  
  47.  
  48. static void *_allocate( unsigned amount )
  49.     {
  50.     void *p;
  51.  
  52. #if defined(__386__)
  53.     p = malloc( amount );
  54. #else
  55.     p = _nmalloc( amount );
  56.     #if defined(__COMPACT__) || defined(__LARGE__) || defined(__HUGE__)
  57.     if( (void near *) p == NULL )  p = malloc( amount );
  58.     #endif
  59. #endif
  60.     if( p == NULL )  _Not_Enough_Memory();
  61.     return( p );
  62.     }
  63.  
  64.  
  65. static int _make_argv( char *p, char ***argv )
  66.     {
  67.     int        argc;
  68.     char        *start;
  69.     char        *new_arg;
  70.     char        wildcard;
  71.     char        lastchar;
  72.     DIR *        dir;
  73.     struct dirent * dirent;
  74.     char        drive[_MAX_DRIVE];
  75.     char        directory[_MAX_DIR];
  76.     char        name[_MAX_FNAME];
  77.     char        extin[_MAX_EXT];
  78.     char        pathin[_MAX_PATH];
  79.  
  80.     argc = 1;
  81.     for(;;) {
  82.         while( *p == ' ' ) ++p;    /* skip over blanks */
  83.         if( *p == '\0' ) break;
  84.         /* we are at the start of a parm */
  85.         wildcard = 0;
  86.         if( *p == '\"' ) {
  87.         p++;
  88.         new_arg = start = p;
  89.         for(;;) {
  90.             /* end of parm: NULLCHAR or quote */
  91.             if( *p == '\"' ) break;
  92.             if( *p == '\0' ) break;
  93.             if( *p == '\\' ) {
  94.             if( p[1] == '\"'  ||  p[1] == '\\' )  ++p;
  95.             }
  96.             *new_arg++ = *p++;
  97.         }
  98.         } else {
  99.         new_arg = start = p;
  100.         for(;;) {
  101.             /* end of parm: NULLCHAR or blank */
  102.             if( *p == '\0' ) break;
  103.             if( *p == ' ' ) break;
  104.             if(( *p == '\\' )&&( p[1] == '\"' )) {
  105.             ++p;
  106.             } else if( *p == '?'  ||  *p == '*' ) {
  107.             wildcard = 1;
  108.             }
  109.             *new_arg++ = *p++;
  110.         }
  111.         }
  112.         *argv = (char **) realloc( *argv, (argc+2) * sizeof( char * ) );
  113.         if( *argv == NULL )  _Not_Enough_Memory();
  114.         (*argv)[ argc ] = start;
  115.         ++argc;
  116.         lastchar = *p;
  117.         *new_arg = '\0';
  118.         ++p;
  119.         if( wildcard ) {
  120.         /* expand file names */
  121.         dir = opendir( start );
  122.         if( dir != NULL ) {
  123.             --argc;
  124.             _splitpath( start, drive, directory, name, extin );
  125.             for(;;) {
  126.             dirent = readdir( dir );
  127.             if( dirent == NULL ) break;
  128.             if( dirent->d_attr &
  129.               (_A_HIDDEN+_A_SYSTEM+_A_VOLID+_A_SUBDIR) ) continue;
  130.             _splitpath( dirent->d_name, NULL, NULL, name, extin );
  131.             _makepath( pathin, drive, directory, name, extin );
  132.             *argv = (char **) realloc( *argv, (argc+2) * sizeof( char * ) );
  133.             if( *argv == NULL )  _Not_Enough_Memory();
  134.             new_arg = (char *) _allocate( strlen( pathin ) + 1 );
  135.             strcpy( new_arg, pathin );
  136.             (*argv)[argc++] = new_arg;
  137.             }
  138.             closedir( dir );
  139.         }
  140.         }
  141.         if( lastchar == '\0' ) break;
  142.     }
  143.     return( argc );
  144.     }
  145.  
  146.  
  147. #ifdef __cplusplus
  148. extern "C"
  149. #endif
  150. void __Init_Argv()
  151.     {
  152.     _argv = (char **) _allocate( 2 * sizeof( char * ) );
  153.     _argv[0] = _LpPgmName;    /* fill in program name */
  154.     _argc = _make_argv( _LpCmdLine, &_argv );
  155.     _argv[_argc] = NULL;
  156.     ___Argc = _argc;
  157.     ___Argv = _argv;
  158.     }
  159.